﻿using System;
using System.Collections.Generic;
using System.Text;
using VietKinsNutrition.Data;
using System.Windows.Forms;
using System.Data;
using DevExpress.XtraEditors;

namespace VietKinsNutrition.Bussiness
{
    public class ClsTblDm_BuaantrongngayBLL
    {
        private int _id;
        public int Id
        {
            get { return _id; }
            set { _id = value; }
        }
        ClsTblDm_BuaantrongngayDAL ClsTblDm_Buaantrongngaydal = new ClsTblDm_BuaantrongngayDAL();
        public bool FindSameName(int iBtn, string VarTen)
        {
            bool Value = false;
            DataTable dt;
            dt = this.ClsTblDm_Buaantrongngaydal.FindSameName(VarTen).Tables[0];
            switch (iBtn)
            {
                case 1:
                    {
                        if (dt.Rows.Count >= 1)
                        {
                            XtraMessageBox.Show("Không cho phép thêm trùng dữ liệu", "Chú ý", MessageBoxButtons.OK, MessageBoxIcon.Question);
                            Value = false;
                        }
                        else
                        {

                            Value = true;
                        }
                        break;
                    }
                case 2:
                    {
                        if (dt.Rows.Count == 0)
                        {
                            return true;
                        }
                        DataRow Row = dt.Rows[0];

                        if (dt.Rows.Count >= 1 && this.Id != int.Parse(Row["Id"].ToString()))
                        {
                            XtraMessageBox.Show("Tên không cho phép trùng", "Chú ý", MessageBoxButtons.OK, MessageBoxIcon.Question);
                            Value = false;
                        }
                        else
                        {
                            Value = true;
                        }
                        break;
                    }
            }
            return Value;
        }
        public void FocusControl(TextBox Textbox, DataGridView Grid)
        {
            Textbox.Focus();
            Grid.Enabled = !Grid.Enabled;
        }
        public void BingSource_InputControl(TextBox Tenbuaan, TextBox Phanbotienan, DataGridView Grid)
        {
            if (Grid.RowCount > 0)
            {
                this.Id = int.Parse(Grid.Rows[Grid.CurrentRow.Index].Cells["Id"].Value.ToString());
                Tenbuaan.Text = Grid.Rows[Grid.CurrentRow.Index].Cells["Tenbuaan"].Value.ToString();
                Phanbotienan.Text = Grid.Rows[Grid.CurrentRow.Index].Cells["Phanbotienan"].Value.ToString();
            }
        }
        public void ClearControl(TextBox Tenbuaan, TextBox Phanbotienan)
        {
            Tenbuaan.Text = "";
            Phanbotienan.Text = "";

        }
        public void BingSource_DataGrid(DataGridView GridView)
        {
            GridView.DataSource = this.ClsTblDm_Buaantrongngaydal.SelectAll();
        }
        public void BingSource_DataGridThucdontuan(DataGridView GridView, int KhoilopId, DateTime Value)
        {
            GridView.DataSource = this.ClsTblDm_Buaantrongngaydal.ThucdontuanSelectAllByKhoilopIdandDate(KhoilopId, Value);
        }
        public void Insert(string Tenbuaan, int Phanbotienan)
        {
            ClsTblDm_Buaantrongngaydal.Insert(Tenbuaan, Phanbotienan);
        }
        public void Edit(int Id, string Tenbuaan, int Phanbotienan)
        {
            ClsTblDm_Buaantrongngaydal.Edit(Id, Tenbuaan, Phanbotienan);
        }
        public void Del()
        {
            ClsTblDm_Buaantrongngaydal.Del(this.Id);
        }
        public static DataTable GetAll()
        {
            return ClsTblDm_BuaantrongngayDAL.GetAll();
        }
    }
}
